package cn.darkhorse.pick.service.impl;

import cn.darkhorse.pick.pojo.PickOrder;
import cn.darkhorse.pick.mapper.PickOrderMapper;
import cn.darkhorse.pick.service.PickOrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangtianqi
 * @since 2025-07-08
 */
@Service
public class PickOrderServiceImpl extends ServiceImpl<PickOrderMapper, PickOrder> implements PickOrderService {

    @Override
    public List<PickOrder> listAll(PickOrder pickOrder) {
        LambdaQueryWrapper<PickOrder> queryWrapper = new LambdaQueryWrapper<>();
        if (pickOrder.getOrderNumber() != null) {
            queryWrapper.like(PickOrder::getOrderNumber, pickOrder.getOrderNumber());
        }
        if (pickOrder.getDeviceCode() != null) {
            queryWrapper.like(PickOrder::getDeviceCode, pickOrder.getDeviceCode());
        }
        if (pickOrder.getDeviceName() != null) {
            queryWrapper.like(PickOrder::getDeviceName, pickOrder.getDeviceName());
        }
        if (pickOrder.getFarmersName() != null) {
            queryWrapper.like(PickOrder::getFarmersName, pickOrder.getFarmersName());
        }
        if (pickOrder.getFarmersNumber() != null) {
            queryWrapper.like(PickOrder::getFarmersNumber, pickOrder.getFarmersNumber());
        }
        if (pickOrder.getStatus() != null) {
            queryWrapper.eq(PickOrder::getStatus, pickOrder.getStatus());
        }
        if (pickOrder.getCollectStartTimeBegin() != null){
            queryWrapper.ge(PickOrder::getCollectStartTime, pickOrder.getCollectStartTimeBegin());
        }
        if (pickOrder.getCollectStartTimeEnd() != null){
            queryWrapper.le(PickOrder::getCollectStartTime, pickOrder.getCollectStartTimeEnd());
        }
        if (pickOrder.getCollectEndTimeBegin() != null){
            queryWrapper.ge(PickOrder::getCollectEndTime, pickOrder.getCollectEndTimeBegin());
        }
        if (pickOrder.getCollectEndTimeEnd() != null){
            queryWrapper.le(PickOrder::getCollectEndTime, pickOrder.getCollectEndTimeEnd());
        }
        if (pickOrder.getPayTimeBegin() != null){
            queryWrapper.ge(PickOrder::getPayTime, pickOrder.getPayTimeBegin());
        }
        if (pickOrder.getPayTimeEnd() != null){
            queryWrapper.le(PickOrder::getPayTime, pickOrder.getPayTimeEnd());
        }
        return list(queryWrapper);
    }

    @Override
    public String getOrderNo() {
        String code = "SDMT";
        String year= String.valueOf(LocalDateTime.now().getYear());
        String month= String.valueOf(LocalDateTime.now().getMonthValue());
        String day= String.valueOf(LocalDateTime.now().getDayOfMonth());
        LambdaQueryWrapper<PickOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(PickOrder::getOrderNumber, code + year + month + day);
        long count = count(queryWrapper);
        return code + year + month + day + String.format("%04d", count + 1);

    }
}
